/**
 * Codigo original:
 * https://github.com/javipacheco/Android-DataFramework/wiki/Ejemplo-de-ListActivity
 */

package com.activity;

import com.container.*;

import java.util.ArrayList;

import com.container.LibroAdapter;
import com.container.MenuEncapsulado;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;



//public class BusqResultActivity extends ListActivity implements LoaderCallbacks<Cursor> {
public class BusqResultActivity extends ListActivity {	
	private LibroAdapter lAdap;
	private ListActivity listact;
	
	private MenuEncapsulado menuEncapsulado;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		listact = this;
		setContentView(R.layout.activity_busq_result);
		Data.listLibros = new ArrayList<Libro>();
		lAdap = new LibroAdapter(getApplicationContext(), Data.listLibros);
		setListAdapter(lAdap);
		
		Consulta con = new Consulta(getApplicationContext(), listact, "buscarLibros"); 
		con.query("__BUSQRESULT__", getIntent().getExtras().getString("query"));
		
	}
	
//	@Override
//	public void onContentChanged() {
//		
//		LibroAdapter adap = lAdap;
//		if (adap!= null){ 
//				for(int i=0; i< adap.getCount(); i++){ Log.d("BusqResultActivity", "adap["+i+"]= "+ adap.getItem(i).getNombre());}
////				((LibroAdapter)this.getListView().getAdapter()).notifyDataSetChanged();
//		}
//		else{	
//			Log.d("ABCD", "adap is NULL");
//			Log.d("BusqResultActivity", "adap is NULL");
//		}
//		super.onContentChanged();
//		
//	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menuEncapsulado = new MenuEncapsulado(menu,this);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(menuEncapsulado.onOptionsItemSelected(item)){
			return true;
		}
		else{
			// Por si es necesario comprobar otro menus
			return false;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
//		Toast.makeText(this, "Id: " + lAdap.getItem(position), Toast.LENGTH_LONG).show();
		Intent mIntent = new Intent(getApplicationContext(), LibroInfoActivity.class);
    	mIntent.putExtra("position", position);
    	mIntent.putExtra("from", "listaLibros");
    	startActivity(mIntent); 
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}
}